package cn.memorynine.consumer.service;

import cn.hutool.core.bean.BeanUtil;
import cn.memorynine.dao.entity.Consumer;
import cn.memorynine.dao.entity.Forum;
import cn.memorynine.dao.entity.ForumPost;
import cn.memorynine.dao.entity.ForumPostReply;
import cn.memorynine.dao.mapper.ConsumerMapper;
import cn.memorynine.dao.mapper.ForumMapper;
import cn.memorynine.dao.mapper.ForumPostMapper;
import cn.memorynine.dao.mapper.ForumPostReplyMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import cn.memorynine.common.model.ResponseWrapper;
import cn.memorynine.consumer.model.forum.*;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
* 用户前台-论坛服务
* @author 代码生成器
*/
@Slf4j
@Service
public class ForumService {

    @Autowired
    private ForumMapper forumMapper;

    @Autowired
    private ForumPostMapper forumPostMapper;

    @Autowired
    private ForumPostReplyMapper forumPostReplyMapper;

    @Autowired
    private ConsumerMapper consumerMapper;

    /**
     * 1.查看论坛列表
     * @return
     */
    public ResponseWrapper forumList(ForumListParam param){
        //TODO 请在此处编写业务逻辑

        //最后一步，返回结果
        return ResponseWrapper.markSuccess();
    }

    /**
     * 2.查看论坛信息
     * @return
     */
    public ResponseWrapper forumInfo(ForumInfoParam param){
        ForumInfoResult forumInfoResult = new ForumInfoResult();

        Forum forum = forumMapper.selectOne(
                Wrappers.<Forum>lambdaQuery()
                        .eq(Forum::getAid, param.getAid())
        );

        BeanUtil.copyProperties(forum,forumInfoResult);


        //最后一步，返回结果
        return ResponseWrapper.markSuccess(forumInfoResult);
    }

    /**
     * 3.查看帖子列表
     * @return
     */
    public ResponseWrapper postList(PostListParam param){
        PostListResult postListResult = new PostListResult();

        Page<ForumPost> pageInfo = new Page<>(param.getPageNum(),param.getNum(),true);
        Page<ForumPost> forumPostPage = forumPostMapper.selectPage(pageInfo,
                Wrappers.<ForumPost>lambdaQuery()
                        .eq(ForumPost::getForumAid, param.getAid())
                        .eq(ForumPost::getIsDelete,false)
                        .like(ForumPost::getTitle, param.getSearchKey())
                        .orderByDesc(ForumPost::getTimeCreate)
        );
        postListResult.setTotalNum((int) forumPostPage.getTotal());
        List<ForumPost> forumPosts = forumPostPage.getRecords();
        List<PostList> postList = new ArrayList<>();

        for (ForumPost forumPost : forumPosts) {
            PostList postListItem = new PostList();
            BeanUtil.copyProperties(forumPost,postListItem);
            String name = consumerMapper.selectOne(Wrappers.<Consumer>lambdaQuery().eq(Consumer::getAid, forumPost.getConsumerAid())).getName();
            postListItem.setConsumerName(name);
            postList.add(postListItem);
        }
        postListResult.setPostList(postList);
        //最后一步，返回结果
        return ResponseWrapper.markSuccess(postListResult);
    }

    /**
     * 4.查看帖子详情
     * @return
     */
    public ResponseWrapper postInfo(PostInfoParam param){
        PostInfoResult postInfoResult = new PostInfoResult();
        ForumPost forumPost = forumPostMapper.selectOne(
                Wrappers.<ForumPost>lambdaQuery()
                        .eq(ForumPost::getAid, param.getAid())
        );
        BeanUtil.copyProperties(forumPost,postInfoResult);
        String name = consumerMapper.selectOne(Wrappers.<Consumer>lambdaQuery().eq(Consumer::getAid, forumPost.getConsumerAid())).getName();
        postInfoResult.setConsumerName(name);

        //最后一步，返回结果
        return ResponseWrapper.markSuccess(postInfoResult);
    }

    /**
     * 5.评论帖子
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseWrapper postComment(PostCommentParam param){
        ForumPostReply forumPostReply = new ForumPostReply();

        forumPostReply.setPostAid(param.getAid());
        forumPostReply.setConsumerAid(param.getTokenConsumerAid());
        forumPostReply.setContent(param.getContent());
        forumPostReply.setIsDelete(false);
        forumPostReplyMapper.insert(forumPostReply);

        //最后一步，返回结果
        return ResponseWrapper.markSuccess();
    }

    /**
     * 6.删除评论
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseWrapper delComment(DelCommentParam param){
        ForumPostReply forumPostReply = new ForumPostReply();

        forumPostReply.setAid(param.getAid());
        forumPostReply.setIsDelete(true);
        forumPostReplyMapper.updateById(forumPostReply);
        //最后一步，返回结果
        return ResponseWrapper.markSuccess();
    }

    /**
     * 7.删除帖子
     * @return
     */
    public ResponseWrapper delPost(DelPostParam param){
        ForumPost forumPost = new ForumPost();
        forumPost.setAid(param.getAid());
        forumPost.setIsDelete(false);

        forumPostMapper.updateById(forumPost);

        //最后一步，返回结果
        return ResponseWrapper.markSuccess();
    }

    /**
     * 8.获取帖子下的评论
     * @return
     */
    public ResponseWrapper replyList(ReplyListParam param){
        ReplyListResult replyListResult = new ReplyListResult();

        Page<ForumPostReply> pageInfo = new Page<>(param.getPageNum(),param.getNum(),true);
        Page<ForumPostReply> forumPostReplyPage = forumPostReplyMapper.selectPage(pageInfo,
                Wrappers.<ForumPostReply>lambdaQuery()
                        .eq(ForumPostReply::getPostAid, param.getAid())
                        .eq(ForumPostReply::getIsDelete,false)
        );
        //设置总数
        replyListResult.setTotalNum((int) forumPostReplyPage.getTotal());

        List<ForumPostReply> forumPostReplies = forumPostReplyPage.getRecords();

        List<ReplyList> replyLists = new ArrayList<>();

        for (ForumPostReply forumPostReply : forumPostReplies) {
            ReplyList replyList = new ReplyList();
            BeanUtil.copyProperties(forumPostReply,replyList);
            //查询回复用户昵称
            String name = consumerMapper.selectOne(Wrappers.<Consumer>lambdaQuery().eq(Consumer::getAid, forumPostReply.getConsumerAid())).getName();
            replyList.setConsumerName(name);
            replyLists.add(replyList);
        }

        replyListResult.setReplyList(replyLists);
        //最后一步，返回结果
        return ResponseWrapper.markSuccess(replyListResult);
    }

    /**
     *9.发布帖子
     */
    public ResponseWrapper savePost(SavePostParam param){
        ForumPost forumPost = new ForumPost();
        BeanUtil.copyProperties(param,forumPost);
        forumPost.setConsumerAid(param.getTokenConsumerAid());
        if (forumPost.getContent().length() >= 50){
            forumPost.setBriefContent(forumPost.getContent().substring(0,50));
        } else {
            forumPost.setBriefContent(forumPost.getContent());
        }
        forumPost.setIsDelete(false);

        return ResponseWrapper.markSuccess();
    }

}